<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Jobprofile extends CI_Controller
{


	function __construct()
	{
		parent::__construct();
		// check if user logged in
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');
		}
		$this->load->library('form_validation');
		$this->load->model('founder/jobprofile_model');
	}
	
	function addprofile($id=null)
	{
		if(isset($id))
		{
			$user= $id;
		}else
		{
			$user=$this->session->userdata('user_id');		
		}
		$data['userid'] = $user;
		//$data['userid'] = $id;
		$data['users_get'] = $this->jobprofile_model->users_get();
		$data['query'] = $this->jobprofile_model->designation_getall();
		$data['query2'] = $this->jobprofile_model->hobbies_getall();
		$meta['page_title'] = "Edit Job Profile";
		$this->load->view('common/header',$meta);
		$this->load->view('founder/add-jobprofile',$data);
		$this->load->view('common/footer');
		//echo $id;
		//exit;
		
	}
	function updateprofile($userid)
	{
		$this->load->view('founder/add-jobprofile');
	}
	function deleteprofile($userid)
	{
		$this->load->view('founder/add-jobprofile');
	}
	function addskills()
	{
		$data['query'] = $this->jobprofile_model->designation_getall();
		$meta['page_title'] = "Add a skill";
		$this->form_validation->set_rules('skillname', 'Name', 'required|is_unique[skills.name]');
		$this->form_validation->set_rules('designation_id', 'Designation', 'required');
		
		$config['upload_path'] = $this->config->base_url().'assets/images/skills/';

			
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';

		
		
		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
		}
		else
		{
			$data['upload_data'] = ($this->upload->data());
			
	$this->jobprofile_model->skills_insert($this->input->post('designation_id'),$this->input->post('skillname'),$data['upload_data' ]['file_name']);
		}
		$data['query'] = $this->jobprofile_model->designation_getall();
		$data['getskills'] = $this->jobprofile_model->skills_getall();
		$this->load->view('common/header',$meta);
		$this->load->view('founder/add-skills',$data);
		$this->load->view('common/footer');

	}
	
	function addhobbies()
	{
		$data['query2'] = $this->jobprofile_model->hobbies_getall();
		$meta['page_title'] = "Add hobbies";
		$this->form_validation->set_rules('hobbyname', 'Name', 'required|is_unique[hobbies.name]');
		//$this->load->model('founder/jobprofile_model');
		$config['upload_path'] = './assets/images/hobbies/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
//exit;
		
		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
		}
		else
		{
			$data['upload_data'] = ($this->upload->data());
			
	$this->jobprofile_model->hobbies_insert($this->input->post('hobbyname'),$data['upload_data' ]['file_name']);
		}
		$data['query2'] = $this->jobprofile_model->hobbies_getall();
		$this->load->view('common/header',$meta);
		$this->load->view('founder/add-hobbies',$data);
		$this->load->view('common/footer');

	}

	

	
	function addefficiency()
	{
		$data['query'] = $this->jobprofile_model->designation_getall();
		$meta['page_title'] = "Add Efficiency";
		$this->form_validation->set_rules('efficiencyname', 'Name', 'required|is_unique[efficiency.name]');
		$this->form_validation->set_rules('designation_id', 'Designation', 'required');						
		if ($this->form_validation->run() == TRUE)
		{
		$insert = array('user_type_id'=>$this->input->post('designation_id'),'name'=>$this->input->post('efficiencyname'),'subfields'=>$this->input->post('efficiencyfields'));	
		$this->jobprofile_model->efficiency_insert($insert);
		redirect('founder/jobprofile/addefficiency');
		//$this->load->model('founder/jobprofile_model');
		
		}
		$data['query'] = $this->jobprofile_model->designation_getall();
		$data['getefficiency'] = $this->jobprofile_model->efficiency_getall();
		$data['efficiency'] = $this->jobprofile_model->get_efficiency_by_user_type_id();
		$this->load->view('common/header',$meta);
		$this->load->view('founder/add-efficiency',$data);
		$this->load->view('common/footer');

	}
	function delete_efficiency($delete_id)
	{
		$this->jobprofile_model->efficiency_delete($delete_id);
		redirect('founder/jobprofile/addefficiency','refresh');
	}
	function delete_skills($delete_id)
	{
		$this->jobprofile_model->skills_delete($delete_id);
		redirect('founder/jobprofile/addskills','refresh');
	}
	function delete_hobbies($delete_id)
	{
		$this->jobprofile_model->hobbies_delete($delete_id);
		redirect('founder/jobprofile/addhobbies','refresh');
	}
	function delete_tools($delete_id)
	{
		$this->jobprofile_model->tools_delete($delete_id);
		redirect('founder/jobprofile/addtools','refresh');
	}
	function addtools()
	{
		$data['query'] = $this->jobprofile_model->designation_getall();
		$meta['page_title'] = "Add Tools";
		$this->form_validation->set_rules('toolname', 'Name', 'required|is_unique[efficiency.name]');
		$this->form_validation->set_rules('designation_id', 'Designation', 'required');
						
		if ($this->form_validation->run() == TRUE)
		{
		$insert = array('user_type_id'=>$this->input->post('designation_id'),'name'=>$this->input->post('toolname'));	
		$this->jobprofile_model->tools_insert($insert);
		redirect('founder/jobprofile/addtools');
				
		}
		$data['query'] = $this->jobprofile_model->designation_getall();
		$data['gettools'] = $this->jobprofile_model->tools_getall();
		$this->load->view('common/header',$meta);
		$this->load->view('founder/add-tools',$data);
		$this->load->view('common/footer');
	}
	function get_efficiency_by_user_type_id()
	{
		$user_type_id = $_REQUEST['user_type'];
		$default_options  =  $this->jobprofile_model->get_efficiency_by_user_type($user_type_id);
		//print_r($default_options);
		
		$j=1;
		foreach($default_options as $default_option)
		{
			
			echo '<input type="text" name="efficiencyname[]" value="'.$default_option->name.'">';
			echo ' <input type="text" name="efficiencyfields[]" value="'.$default_option->subfields.'">';
			echo ' <input type="text" id="id'.$j++.'" placeholder="%" name="efficiencylevel[]" style="width:15px">';
			echo "<br>";
		}
		
	}
	
	function get_skills_by_user_type_id()
	{
		$user_type_id = $_REQUEST['user_type'];
		//exit;
		$default_options  =  $this->jobprofile_model->get_skills_by_user_type_id($user_type_id);

		foreach($default_options as $default_option)
		{
			echo '<input type="hidden" name="skillid[]" value="'.$default_option->id.'" >';
			echo '<input type="text" name="skillname[]" value="'.$default_option->name.'">';
			echo ' <input type="text" name="skilllevel[]" placeholder="%" onkeyup="check(this)" style="width:20px">';
			echo "<br>";
		}
	}
	function get_tools_by_user_type_id()
	{
		$user_type_id = $_REQUEST['user_type'];
		//exit;
		$default_options  =  $this->jobprofile_model->get_tools_by_user_type_id($user_type_id);
		//print_r($default_options);
		foreach($default_options as $default_option)
		{
			echo '<input type="text" name="toolname[]" value="'.$default_option->name.'">';
			echo ' <input type="text" name="toollevel[]" placeholder="%" onkeyup="check(this)" style="width:20px">';
			echo "<br>";
		}
	}
			
	function getallprofilelist()
	{
		
		//$this->form_validation->set_rules('efficiencyname[]', 'efficiencyname', 'required');
		$this->form_validation->set_rules('user_type', 'Designation', 'required');
		if ($this->form_validation->run() == TRUE)
		{		
		
			$user=$this->session->userdata('user_id');	
		
		
			$users = $this->input->post('users');	
			$efficiencyname = $this->input->post('efficiencyname');
			$efficiencyfields = $this->input->post('efficiencyfields');
			$efficiencylevel = $this->input->post('efficiencylevel');
			
			$combine_efficiency = array();
			for($x=0;$x<count($efficiencyname);$x++)
			{
				$combine_efficiency[$x]['user_id'] = $users;
				$combine_efficiency[$x]['name'] = $efficiencyname[$x];
				$combine_efficiency[$x]['custom_subfields'] = $efficiencyfields[$x];
				$combine_efficiency[$x]['level'] = $efficiencylevel[$x];
			}
			//print_r($combine_efficiency);exit;
								
		$this->jobprofile_model->efficiency_custom_insert($combine_efficiency);
		
			$skillid = $this->input->post('skillid');	
			$skillname = $this->input->post('skillname');
			$skilllevel = $this->input->post('skilllevel');
			
			$combine_skill = array();
			
			for($x=0;$x<count($skillname);$x++)
			{
				$combine_skill[$x]['user_id'] = $users;
				$combine_skill[$x]['skills_id'] = $skillid[$x];
				$combine_skill[$x]['name'] = $skillname[$x];
				$combine_skill[$x]['level'] = $skilllevel[$x];
			}
		//print_r($combine_skill);exit;
								
		$this->jobprofile_model->skill_custom_insert($combine_skill);
		
			$toolname = $this->input->post('toolname');
			$toollevel = $this->input->post('toollevel');
			
			$combine_tool = array();
			
			for($x=0;$x<count($toolname);$x++)
			{
				$combine_tool[$x]['user_id'] = $users;
				$combine_tool[$x]['name'] = $toolname[$x];
				$combine_tool[$x]['level'] = $toollevel[$x];
			}
								
		$this->jobprofile_model->tool_custom_insert($combine_tool);
		
		
		
			
			$hobbiesid = $this->input->post('hobbies');
			if(!empty($hobbiesid))
			{
				
			$combine_hobbies = array();
			
			for($x=0;$x<count($hobbiesid);$x++)
			{
				$combine_hobbies[$x]['user_id'] = $users;
				$combine_hobbies[$x]['hobbies_id'] = $hobbiesid[$x];
			}
	
								
		$this->jobprofile_model->user_hobbies_insert($combine_hobbies);
		}
		redirect('user/jobprofile/viewprofile/'.$users);
		}		
		else
		{
		redirect('founder/jobprofile/addprofile/');
		}
			
	}
	
	function viewprofile($id)
	{
		$data['userid'] = $id;
		//echo $id;
		//exit;
		//$users = $_REQUEST['user'];
		$data['getefficiencyid'] = $this->jobprofile_model->efficiency_getall_by_id($id);
		$data['efficiencychart'] = $this->jobprofile_model->efficiencychart($id);
		$data['efficiencyfields'] = $this->jobprofile_model->efficiencyfields($id);
		$data['skillschart'] = $this->jobprofile_model->skillschart($id);
		$data['toolschart'] = $this->jobprofile_model->toolschart($id);
		$data['hobbies'] = $this->jobprofile_model->hobbies_getall_by_id($id);
		$data['hobbyimage'] = $this->jobprofile_model->hobbyimage($id);
		$data['userdetails'] = $this->jobprofile_model->userdetails($id);
		$data['designationbyid'] = $this->jobprofile_model->designationbyid($id);
		$data['skillsimage'] = $this->jobprofile_model->skillsimage($id);
		$data['getskillsid'] = $this->jobprofile_model->skills_getall_by_id($id);
		$data['gettoolsid'] = $this->jobprofile_model->tools_getall_by_id($id);
		//$data['query'] = $this->jobprofile_model->tools_getall();
		//$data['query'] = $this->jobprofile_model->hobbies_getall();

		$meta['page_title'] = "View Job Profile";
		$this->load->view('common/header',$meta);
		$this->load->view('founder/view-jobprofile',$data);
		$this->load->view('common/footer');
		//echo $id;

	}
	

	}